<?php

header("Content-type: text/html; charset=utf-8");
include './function.php';
include './GlobalUniqueId.php';
include './WeightedRoundRobin.php';
include './BubbleSort.php';
include './BinarySearch.php';
include './Random.php';
include './BitOperation.php';
include './Spl.php';

$start_time = microtime(true);

//$a1 = 50;
//$a2 = -50;
//p(decbin(-50));
//p(decbin($a2));
//p(mb_strlen(decbin($a2)));
//die;
//p($a2);
//p(base_convert($a1, 10, 2));
//pp(base_convert($a2, 10, 2));
//$user_suf_arr = [];
//for ($i = 1; $i <= 100000; $i++) {
//
//    // 随机一个数值 user后缀
//    $user_suf = mt_rand(1, 2000000);
//    // 如果在数组中
//    if (in_array($user_suf, $user_suf_arr)) {
//        echo '重复，回滚' . PHP_EOL;
//        // 回滚
//        $i--;
//        continue;
//    }
//    $user_suf_arr[] = $user_suf;
//
//    $user[$i] = ['name' => 'user' . $user_suf, 'age' => mt_rand(1, 100), 'gender' => mt_rand(1, 2),];
//    $uid_index_arr[$user_suf] = $i;
//}
//$user = json_encode($user);
//file_put_contents('./user.json', $user);
//file_put_contents('./user.index.json', json_encode($uid_index_arr));
//$begin_time = microtime(true);
//// 读取数据
//$user = file_get_contents('./user.json');
//$user = json_decode($user, true);
//// 读取索引
//$index = file_get_contents('./user.index.json');
//$index = json_decode($index, true);
//// 假如很短时间内500个人并发查询
//// 查找同一个用户名不会有缓存，所以不用考虑缓存影响速度的问题
//for ($i = 1; $i <= 500; $i++) {
//    $suf = substr('user1948829', 4);
//    // 在索引文件中查找对应关系
//    $_index = $index[$suf];
//    $_user = $user[$_index];
//    echo $_user['age'] . PHP_EOL;
//}
//$end_time = microtime(true);
//echo '耗费时间' . ( $end_time - $begin_time ) . PHP_EOL;
//pp($start_time);
//$tt = '5c4138ef4';
//p(mb_strlen($tt));
//pp($start_time);
//$res = GlobalUniqueId::CreateBasicsUid(1);
//$rr = '201901100011475487669';
//pp(mb_strlen($rr));
//$tr = uniqid();
//$rr =  Random::machineRandom();
//p(mb_strlen($rr));
//pp($rr);
//$str = '123456789';
//pp(mb_substr($str, -4, 4));
//$rr = Random::BasicsRandom();
//p(mb_strlen($rr));
//pp($rr);
//for ($i = 0; $i <= 100; $i++) {
//    $rr = Random::getRandomNumber();
//    p(mb_strlen($rr));
//    p($rr);
//}
//die;
//$oo = GlobalUniqueId::CharacterToNumber($tr);
//p($tr);
//pp($oo);
//$tt = GlobalUniqueId::CreateBasicsUid(1);
//p(mb_strlen($tt));
//pp($tt);
//pp(microtime(true));
//$rr = GlobalUniqueId::microtime_format('Y-m-d H:i:s x', microtime(true));
//pp($rr);
//轮训算法
//命中测试比例
//$arr = array(
//    array('id' => 'A', 'weight' => 2),
//    array('id' => 'B', 'weight' => 3),
//    array('id' => 'C', 'weight' => 5),
//);
//$arr = array(
//    array('id' => 'A', 'weight' => 2),
//    array('id' => 'B', 'weight' => 3),
//    array('id' => 'C', 'weight' => 6),
//    array('id' => 'D', 'weight' => 6),
//    array('id' => 'E', 'weight' => 4),
//);
//$weight = new WeightedRoundRobin($arr);
//$a = 0;
//$b = 0;
//$c = 0;
//$d = 0;
//$e = 0;
//for ($j = 0; $j < 100; $j++) {
//    $weightInfo = $weight->getWeight();
////    print_r($weightInfo);
//    echo $weightInfo['id'] . '----------------------weight:' . $weightInfo['weight'] . '<br/>';
//    if ($weightInfo['id'] == 'A') {
//        $a++;
//    }
//    if ($weightInfo['id'] == 'B') {
//        $b++;
//    }
//    if ($weightInfo['id'] == 'C') {
//        $c++;
//    }
//    if ($weightInfo['id'] == 'D') {
//        $d++;
//    }
//    if ($weightInfo['id'] == 'E') {
//        $e++;
//    }
//}
//echo 'A:' . $a . '<br/>';
//echo 'B:' . $b . '<br/>';
//echo 'C:' . $c . '<br/>';
//echo 'D:' . $d . '<br/>';
//echo 'E:' . $e . '<br/>';
//exit;
//$array = array(0 => 'blue',  2 => 'green', 3 => 'red');
//
//$key = array_search('red', $array);
//
//pp($key);
//$arr = [6, 4, 7, 2, 9, 8, 1, 6, 4, 7, 2, 9, 8, 1];
//
//$rr = BubbleSort::BetterBubbleSort($arr);
//p($rr);
//$rr = BinarySearch::BasicBinarySearch($arr, 8);
//pp($rr);
//$a = 8;
//p(base_convert($a, 10, 2));
//$b = 4;
//p(base_convert($b, 10, 2));
//p($a << $b);
//p(base_convert(128, 10, 2));
//pp(ini_get('Architecture'));
//p(BitOperation::SystemDigit());
//$str = '11111111111111111111111111111111';
//$tt = BitOperation::Format($str);
//p($tt);
//$str = "Hello World";
//echo str_pad($str,30,".");
//die;
//$r = 0;
//$tt = decbin($r);
//$tt = BitOperation::Format($tt);
//p($tt);
//计算一个十进制数转换为二进制数中‘1’的个数
//例如十进制11 = 二进制1011，则结果是3个1
//解题思路：利用 n & (n - 1) 可以将最后一个1变0
//xxxx1000 & (xxxx1000 - 1) = xxxx1000 & xxxx0111 = xxxx0000
// 1011 & (1011 - 1) = 1011 & 1010 = 1010
//直到最后一个1被与为0，得出结果
//p(-2%3);
//$a1 = '0b' . decbin(3);
//pp(~3);
//$a2 = '0b' . decbin(2);
//p($a1);
//p($a2);
//p(bindec($a1));
//$rr = bcadd($a1, $a2);
//pp($rr);
//
//$y = ~$tt;
//pp($y);
//$t = bcadd('1111111111111111111111111111111111111111111111111111111', '222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222');
//pp(gettype($t));
//pp(bcadd('1111111111111111111111111111111111111111111111111111111', '222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222'));
//$tt = -555;
//p(decbin($tt));
//$tt = BitOperation::InverseCode($tt);
//p($tt);
//printf('%064b',$tt);
//$tt = -1;
//p(decbin($tt));
//$t = BitOperation::OriginalCode($tt);
//$a = 7;
//$b = 8;
//pp(BitOperation::average($a, $b));
//BitOperation::swap($a, $b);
//p($a);
//pp($b);
//p(base_convert($a, 10, 2));
//p(base_convert($b, 10, 2));
//p(BitOperation::DecimalToBinary($a));
//p(BitOperation::DecimalToBinary($b));
//p($a & $b);
//p(($a & 0xFFFF) && !($a & 0x8000));
//p(PHP_INT_MAX);
//p(PHP_FLOAT_MAX);
//$max = 1 << 31;
//p($max);
//p(1 >> 31);
//pp($max + 1);
//p(base_convert(0x8000, 16, 2));
//pp(base_convert(0x8000, 16, 2));
//$t = BitOperation::IntAdd($a, $b);
//$t = BitOperation::abs($a);
//$t = BitOperation::isNegative($a);
//pp($a>>31);
/*
 * 双向链表
 */
//$SplDoublyLinkedList = new SplDoublyLinkedList();
//
//$SplDoublyLinkedList->push('hiramariam');
//$SplDoublyLinkedList->push('maaz');
//$SplDoublyLinkedList->push('zafar');
////$s = $SplDoublyLinkedList->serialize();
////$SplDoublyLinkedList->isEmpty()
//p($SplDoublyLinkedList->top());
//pp($SplDoublyLinkedList);
//$s = new SplObjectStorage();
//
//$o1 = new StdClass;
//$o2 = new StdClass;
//$o3 = new StdClass;
//
//$s->attach($o1);
//$s->attach($o2);
//
//var_dump($s->contains($o1));
//var_dump($s->contains($o2));
//var_dump($s->contains($o3));
//
//$s->detach($o2);
//
//var_dump($s->contains($o1));
//var_dump($s->contains($o2));
//var_dump($s->contains($o3));

//trait foo {
//    
//}
//
//class bar {
//
//    use foo;
//}
//
////print_r(class_uses(new bar));
//print_r(class_uses('bar'));


$end_time = microtime(true);
pp('运行时间为' . (sprintf('%0.4f', $end_time - $start_time)) . '毫秒');
